package com.atguigu.q_cas01;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class Num{
    int num;

    // 同步锁
    public int getNum() {
        return num;
    }

    public synchronized void setNum() {
        num++;
    }

    // cas
    AtomicInteger atomicInteger = new AtomicInteger();
    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
        atomicInteger.getAndIncrement();
    }
}

public class Cas02 {
    public static void main(String[] args) {
        Num num = new Num();
        for (int i = 1; i <= 100; i++) {
            new Thread(()->{
                for (int i1 = 1; i1 <= 100; i1++) {
//                    num.setNum();
                    num.setAtomicInteger();
                }
            }, "a").start();
        }

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        System.out.println(Thread.currentThread().getName()+"...."+num.getNum());
        System.out.println(Thread.currentThread().getName()+"...."+num.getAtomicInteger());
    }
}
